package com.cowin.utils;

/**
 * Copyright (C), 1998-2018, 奇瑞信息
 *
 * @author : 于超
 * @Date: 2019/9/7 16:26
 * @Description:
 */


import lombok.extern.slf4j.Slf4j;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC 工具类
 */
@Slf4j
public class JDBCUtils {
    private static Connection conn;
    //    private static String url = "jdbc:sqlserver://10.60.104.17\\durr_systems;DatabaseName=EcoEMOS_Chery_YBP_PS1";
//    private static String user = "chery_mes";
//    private static String password = "chery#Mes2019";
//    private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    /*
    静态代码块加载驱动
     */
    static {
        try {
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    /**
     * 获取连接
     *
     * @return 连接对象
     */
    public static Connection getConnection(String url, String user, String password) {
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
//            e.printStackTrace();
            log.error("获取数据库连接失败" + e.getMessage());
        }
        return conn;
    }

    /**
     * 释放资源
     *
     * @param rs
     * @param st
     * @param conn
     */
    public static void close(ResultSet rs, Statement st, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException("关闭连接失败，请重试");
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                throw new RuntimeException("关闭连接失败，请重试");
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException("关闭连接失败，请重试");
            }
        }
    }
}

